﻿using System;
using System.Diagnostics.CodeAnalysis;
using System.Drawing;

namespace LiveNet.Photos
{
    public class Photo
    {
        private readonly LivePhotos m_Photos;
        private readonly string m_Name;
        private readonly DateTime m_CreatedOn;
        private DateTime m_LastModified;
        private string m_Caption;
        private readonly Uri m_AlbumUrl;
        private readonly Uri m_PhotoUrl;
        private Image m_PhotoImage;

        internal Photo(LivePhotos photosApi, string name, DateTime createdOn, DateTime lastModified, string caption, Uri albumUrl, Uri photoUrl)
        {
            m_Photos = photosApi;
            m_Name = name;
            m_PhotoUrl = photoUrl;
            m_AlbumUrl = albumUrl;
            m_Caption = caption;
            m_LastModified = lastModified;
            m_CreatedOn = createdOn;
        }

        #region Properties
        /// <summary>
        /// Gets the name.
        /// </summary>
        /// <value>The name.</value>
        public string Name
        {
            get { return m_Name; }
        }

        /// <summary>
        /// Gets the created on.
        /// </summary>
        /// <value>The date the photo was created on.</value>
        public DateTime CreatedOn
        {
            get { return m_CreatedOn; }
        }

        /// <summary>
        /// Gets the last modified.
        /// </summary>
        /// <value>The last modified date.</value>
        public DateTime LastModified
        {
            get { return m_LastModified; }
        }

        /// <summary>
        /// Gets the caption.
        /// </summary>
        /// <value>The caption.</value>
        public string Caption
        {
            get { return m_Caption; }
        }

        /// <summary>
        /// Gets the album URL.
        /// </summary>
        /// <value>The album URL.</value>
        public Uri AlbumUrl
        {
            get { return m_AlbumUrl; }
        }

        /// <summary>
        /// Gets the photo URL.
        /// </summary>
        /// <value>The photo URL.</value>
        public Uri PhotoUrl
        {
            get { return m_PhotoUrl; }
        }

        /// <summary>
        /// Gets the photo path.
        /// </summary>
        /// <value>The photo path.</value>
        public string PhotoPath
        {
            get
            {
                return
                    string.Format("{0}{1}", PhotoUrl.Segments[PhotoUrl.Segments.Length - 2],
                                  PhotoUrl.Segments[PhotoUrl.Segments.Length - 1]);
            }
        }

        /// <summary>
        /// Gets the owner.
        /// </summary>
        /// <value>The owner.</value>
        public LivePhotos Owner
        {
            get { return m_Photos; }
        }
        #endregion

        #region Public Methods
        /// <summary>
        /// Gets the image.
        /// </summary>
        /// <returns>An Image of the photo</returns>
        /// <example>
        /// <code>
        /// // Create the instance of LivePhotos
        /// LivePhotos m_spaces = new LivePhotos("example@hotmail.com", DAT, AuthenticationToken.DomainAuthenticationToken);
        /// 
        /// // Get the Photo
        /// Uri photoUrl = new Uri("https://cumulus.services.live.com/example@hotmail.com/myAlbum/winter.jpg");
        /// Photo m_Photo = m_spaces.GetPhoto(photoUrl);
        /// 
        /// Image image = m_Photo.GetImage();
        /// image.Save(string.Format("C:\{0}", m_Photo.Name), ImageFormat.Jpeg);
        /// </code>
        /// </example>
        [SuppressMessage("Microsoft.Design", "CA1024:UsePropertiesWhereAppropriate")]
        public Image GetImage()
        {
            if (m_PhotoImage == null)
            {
                m_PhotoImage = m_Photos.GetImage(m_PhotoUrl);
            }

            return m_PhotoImage;
        }

        /// <summary>
        /// Deletes this instance.
        /// </summary>
        /// <returns></returns>
        /// <example>
        /// <code>
        /// // Create the instance of LivePhotos
        /// LivePhotos m_spaces = new LivePhotos("example@hotmail.com", DAT, AuthenticationToken.DomainAuthenticationToken);
        /// 
        /// // Get the Photo
        /// Uri photoUrl = new Uri("https://cumulus.services.live.com/example@hotmail.com/myAlbum/winter.jpg");
        /// Photo m_Photo = m_spaces.GetPhoto(photoUrl);
        /// 
        /// if(m_Photo.Delete())
        /// {
        ///     Console.WriteLine(string.Format("{0} deleted.", m_Photo.Name);
        /// }
        /// </code>
        /// </example>
        public bool Delete()
        {
            return m_Photos.DeletePhoto(this);
        }

        /// <summary>
        /// Updates the caption.
        /// </summary>
        /// <param name="caption">The new caption.</param>
        /// <example>
        /// <code>
        /// // Create the instance of LivePhotos
        /// LivePhotos m_spaces = new LivePhotos("example@hotmail.com", DAT, AuthenticationToken.DomainAuthenticationToken);
        /// 
        /// // Get the Photo
        /// Uri photoUrl = new Uri("https://cumulus.services.live.com/example@hotmail.com/myAlbum/winter.jpg");
        /// Photo m_Photo = m_spaces.GetPhoto(photoUrl);
        /// 
        /// // Display old caption
        /// Console.WriteLine(string.Format("\"{0}\" is the old caption.", m_Photo.Caption);
        /// string newcaption = "I have had a changed caption";
        /// 
        /// // Update and display new caption
        /// m_Photo.UpdateCaption(newcaption);
        /// Console.WriteLine(string.Format("\"{0}\" is the new caption.", m_Photo.Caption);
        /// </code>
        /// </example>
        public void UpdateCaption(string caption)
        {
            Photo updatedPhoto = m_Photos.UpdatePhoto(this, caption, m_Name);

            if (updatedPhoto != null)
            {
                m_LastModified = updatedPhoto.LastModified;
                m_Caption = updatedPhoto.Caption;
            }
        }

        // Not implemented yet on the server. Uncomment when it is.
        //public void UpdateName(string name)
        //{
        //    Photo updatedPhoto = m_Photos.UpdatePhoto(this, m_Caption, name);

        //    if (updatedPhoto != null)
        //    {
        //        m_LastModified = updatedPhoto.LastModified;
        //        m_Name = updatedPhoto.Name;
        //    }
        //}

        //public void Update(string name, string caption)
        //{
        //    Photo updatedPhoto = m_Photos.UpdatePhoto(this, caption, name);

        //    if (updatedPhoto != null)
        //    {
        //        m_LastModified = updatedPhoto.LastModified;
        //        m_Caption = updatedPhoto.Caption;
        //        m_Name = updatedPhoto.Name;
        //    }
        //}
        #endregion
    }
}
